﻿using System;
using System.Text.RegularExpressions;

namespace ORS.OrderRobot.Service
{
	public static class FlightNoParser
	{

		/// <summary>
		/// 航班号格式正则表达式
		/// 格式：3U0725
		/// 前面两位必须符合字典key的特征，冒号分隔，冒号后面是3-4位数字
		/// </summary>
		private static readonly Regex FlightNoRegex = new Regex(
			@"^([A-Z0-9]{2})(\d{3,4})$",
			RegexOptions.Compiled | RegexOptions.IgnoreCase
		);

		/// <summary>
		/// 校验航班号格式
		/// </summary>
		/// <param name="input">输入的航班号</param>
		/// <returns>校验结果</returns>
		public static (bool validResult,string errMsg ) Validate(string input)
		{
			string errMsg = $"航班号格式不正确：{input}。格式应为：航空公司代码2位、航班号4位，例:3U0725";
			if (!FlightNoRegex.Match(input).Success )
			{
				return (false,errMsg);
			}
			return (true,"");
		}

		/// <summary>
		/// 解析航班号（带冒号格式）
		/// </summary>
		/// <param name="input">格式：ab:ccdx</param>
		/// <returns></returns>
		public static (string airlineCode, string flightNo) Parse(string input)
		{
			var airLine = input.Substring(0,2);
			if(airLine == null)
			{
                throw new ArgumentException("获取不到航空公司编码");
            }
			var airlineCode = airLine.ToUpper();
			var flightNo = input.Substring(2);
			if (flightNo.Length == 3)
			{
				flightNo = "0" + flightNo;
			}
			return (airlineCode, flightNo);
		}
	}
}

